package com.itheima.project.face.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.constant.sms.SmsCacheConstant;
import com.itheima.project.core.enums.sms.SmsBlacklistEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.SmsBlacklistFace;
import com.itheima.project.pojo.SmsBlacklist;
import com.itheima.project.service.ISmsBlacklistService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.sms.SmsBlacklistVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName SmsBlacklistFaceImpl.java
 * @Description 黑名单dubbo实现
 */
@Slf4j
@Component
public class SmsBlacklistFaceImpl implements SmsBlacklistFace {

    @Autowired
    ISmsBlacklistService smsBlacklistService;

    @Override
    @Cacheable(value = SmsCacheConstant.PAGE_BLACKLIST,key ="#pageNum+'-'+#pageSize+'-'+#smsBlacklistVo.hashCode()")
    public Page<SmsBlacklistVo> findSmsBlacklistVoPage(SmsBlacklistVo smsBlacklistVo,
                                                       int pageNum,
                                                       int pageSize)throws ProjectException {
        try {
            Page<SmsBlacklist> page = smsBlacklistService.findSmsBlacklistVoPage(smsBlacklistVo, pageNum, pageSize);
            Page<SmsBlacklistVo> pageVo = BeanConv.toPage(page,SmsBlacklistVo.class);
            return pageVo;
        } catch (Exception e) {
            log.error("查询黑名单列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsBlacklistEnum.PAGE_FAIL);
        }

    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_BLACKLIST,allEntries = true)},
            put={@CachePut(value =SmsCacheConstant.PREFIX_BLACKLIST,key = "#result.id")})
    public SmsBlacklistVo createSmsBlacklist(SmsBlacklistVo smsBlacklistVo)throws ProjectException {
        try {
            return BeanConv.toBean( smsBlacklistService.createSmsBlacklist(smsBlacklistVo), SmsBlacklistVo.class);
        } catch (Exception e) {
            log.error("保存黑名单异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsBlacklistEnum.CREATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_BLACKLIST,allEntries = true),
            @CacheEvict(value =SmsCacheConstant.PREFIX_BLACKLIST,key = "#smsBlacklistVo.id")})
    public SmsBlacklistVo updateSmsBlacklist(SmsBlacklistVo smsBlacklistVo)throws ProjectException {
        try {
            Boolean flag = smsBlacklistService.updateSmsBlacklist(smsBlacklistVo);
            if (flag){
                return smsBlacklistVo;
            }else {
                log.error("保存黑名单异常！");
                throw new ProjectException(SmsBlacklistEnum.UPDATE_FAIL);
            }
        } catch (Exception e) {
            log.error("保存黑名单异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsBlacklistEnum.UPDATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_BLACKLIST,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.PREFIX_BLACKLIST,allEntries = true)})
    public Boolean deleteSmsBlacklist(String[] checkedIds) throws ProjectException{
        try {
            return smsBlacklistService.deleteSmsBlacklist(checkedIds);
        } catch (Exception e) {
            log.error("删除黑名单异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsBlacklistEnum.DELETE_FAIL);
        }
    }

    @Override
    @Transactional
    @Cacheable(value = SmsCacheConstant.PREFIX_BLACKLIST,key = "#smsBlacklistId")
    public SmsBlacklistVo findSmsBlacklistBySmsBlacklistId(Long smsBlacklistId)throws ProjectException {
        try {
            SmsBlacklist smsBlacklist = smsBlacklistService.getById(smsBlacklistId);
            if (!EmptyUtil.isNullOrEmpty(smsBlacklist)){
                return BeanConv.toBean(smsBlacklist,SmsBlacklistVo.class);
            }
            return null;
        } catch (Exception e) {
            log.error("查找黑名单异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsBlacklistEnum.SELECT_FAIL);
        }
    }

}
